home *** CD-ROM | disk | FTP | other *** search
/ PC Direct 1998 August / PC Direct August 1998.iso / S / powerj / Product / hpp.z / WDEBUG.HPP < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-12  |  11.8 KB  |  421 lines

  1. /**********************************************************************
  2.  *
  3.  * WDebug -- Debugging utilties.
  4.  *
  5.  **********************************************************************/
  6.  
  7. #ifndef _WDEBUG_HPP_INCLUDED
  8. #define _WDEBUG_HPP_INCLUDED
  9.  
  10. #ifndef _WNO_PRAGMA_PUSH
  11. #pragma pack(push,8);
  12. #pragma enum int;
  13. #endif
  14.  
  15. #ifndef _WDEF_HPP_INCLUDED
  16. #   include "wdef.hpp"
  17. #endif
  18. #ifndef _WLLIST_HPP_INCLUDED
  19. #   include "wllist.hpp"
  20. #endif
  21.  
  22. //
  23. // WDBG macro -- Use like: WDBG(( "This is number %d", i ));
  24. //
  25.  
  26. #ifdef _DEBUG
  27. #define WDBG(x)    WDebug::Printf x
  28. #else
  29. #define WDBG(x)
  30. #endif
  31.  
  32. //
  33. // WAssert macro -- Use to check that a given condition is true.
  34. //
  35.  
  36. #define WVerify(__x)\
  37.         (((int)(__x))? (void)0                                          \
  38.                      : WDebug::Assert( NULL, __WTEXT(#__x), NULL, \
  39.                                        __WTEXT(__FUNCTION__), \
  40.                                        __WTEXT(__FILE__), __LINE__ ))
  41.  
  42. #define WVerifyEx(__x,__msg)\
  43.         (((int)(__x))? (void)0                                          \
  44.                      : WDebug::Assert( NULL, __WTEXT(#__x), __WTEXT(__msg),\
  45.                                        __WTEXT(__FUNCTION__), \
  46.                                        __WTEXT(__FILE__), __LINE__ ))
  47.  
  48. #ifdef _DEBUG
  49. #define WAssert(__x)\
  50.         (((int)(__x))? (void)0                                          \
  51.                      : WDebug::Assert( NULL, __WTEXT(#__x), NULL, \
  52.                                        __WTEXT(__FUNCTION__), \
  53.                                        __WTEXT(__FILE__), __LINE__ ))
  54.  
  55. #define WAssertEx(__x,__msg)\
  56.         (((int)(__x))? (void)0                                          \
  57.                      : WDebug::Assert( NULL, __WTEXT(#__x), __WTEXT(__msg),\
  58.                                        __WTEXT(__FUNCTION__), \
  59.                                        __WTEXT(__FILE__), __LINE__ ))
  60.  
  61. #define WWarning(__msg)\
  62.             WDebug::Assert( __WTEXT("Warning"),__WTEXT(__msg),NULL,\
  63.                             __WTEXT(__FUNCTION__),\
  64.                                        __WTEXT(__FILE__), __LINE__ )
  65. #else
  66. #define WAssert(__x)
  67. #define WAssertEx(__x,__msg)
  68. #define WWarning(__msg)
  69. #endif
  70.  
  71. #define WVERIFY(__x)          WVerify(__x)
  72. #define WVERIFYEX(__x,__msg)  WVerifyEx(__x,__msg)
  73. #define WASSERT(__x)          WAssert(__x)
  74. #define WASSERTEX(__x,__msg)  WAssertEx(__x,__msg)
  75. #define WWARNING(__msg)       WWarning(__msg)
  76.  
  77.  
  78. #define WInternalError(__msg) WDebug::InternalError( __msg,\
  79.                                               __WTEXT(__FUNCTION__),\
  80.                                               __WTEXT(__FILE__),__LINE__)
  81.  
  82.  
  83. #ifndef _DEBUG
  84. #define __NO_WCM_TRACING__
  85. #endif
  86.  
  87. //
  88. // WTraceMember macro -- Declare at beginning of a WObject member function.
  89. //
  90.  
  91. #ifndef __NO_WCM_TRACING__
  92. #define WTraceMember()    static WTraceMarker __marker(__WTEXT(__FUNCTION__),\
  93.                                                   __WTEXT(__FILE__),\
  94.                                                   __LINE__);\
  95.                           WTrace __trace( __marker );\
  96.                           WValidateObject(this);
  97. #else
  98. #define WTraceMember()
  99. #endif
  100.  
  101. //
  102. // WTraceFunction macro -- Declare at beginning of a non-member function
  103. //                         or of a non-WObject member.
  104. //
  105.  
  106. #ifndef __NO_WCM_TRACING__
  107. #define WTraceFunction()  static WTraceMarker __marker(__WTEXT(__FUNCTION__),\
  108.                                                   __WTEXT(__FILE__),\
  109.                                                   __LINE__);\
  110.                           WTrace __trace( __marker );
  111. #else
  112. #define WTraceFunction()
  113. #endif
  114.  
  115. //
  116. // WTraceReset macro --
  117. //
  118.  
  119. #ifndef __NO_WCM_TRACING__
  120. #define WTraceReset(x)    static WTraceMarker __marker ## x(__WTEXT(__FUNCTION__),\
  121.                                                   __WTEXT(__FILE__),\
  122.                                                   __LINE__);\
  123.                           __trace.Reset( __marker ## x );
  124. #else
  125. #define WTraceReset(x)
  126. #endif
  127.  
  128. //
  129. // WValidateObject macro -- Use to check if object is valid or not.
  130. //
  131.  
  132. #ifdef __BUILDING_WCM__
  133. #define WGETOBJECTFLAGS WDebug::ObjectFlags
  134. #define WGETCHECKGDIHANDLES WDebug::CheckGDIHandles
  135. #define WGETCHECKSTRINGS WDebug::CheckStrings
  136. #define WGETMEMORYTRACKING WDebug::MemoryTracking
  137. #else
  138. #define WGETOBJECTFLAGS WDebug::GetObjectFlags()
  139. #define WGETCHECKGDIHANDLES WDebug::GetCheckGDIHandles()
  140. #define WGETCHECKSTRINGS WDebug::GetCheckStrings()
  141. #define WGETMEMORYTRACKING WDebug::GetMemoryTracking()
  142. #endif
  143.  
  144. #ifdef _DEBUG
  145. #pragma warning 549 9
  146. #define WValidateObject(__x) \
  147.              ((WGETOBJECTFLAGS == 0) ? (void)0 : \
  148.                  WDebug::ValidateObject( (WObject*)__x,\
  149.                                      (((__x)!=NULL)?sizeof(*__x):0),\
  150.                                      __WTEXT(__FUNCTION__), \
  151.                                      __WTEXT(__FILE__),__LINE__))
  152.  
  153. #else
  154. #define WValidateObject(__x)
  155. #endif
  156.  
  157. #define WVALIDATEWOBJECT(__x) WValidateObject(__x)
  158. #define WVALIDATETHIS()       WValidateObject(this)
  159.  
  160. class WObject;
  161.  
  162. //
  163. // WNoDefault macro -- Use to catch unexpected switch values.
  164. //
  165.  
  166. #ifdef _DEBUG
  167. #define WNoDefault() WDebug::NoDefault(__WTEXT(__FUNCTION__),\
  168.                                        __WTEXT(__FILE__),__LINE__ )
  169. #else
  170. #define WNoDefault()
  171. #endif
  172.  
  173. #define WNODEFAULT WNoDefault()
  174.  
  175. #ifdef _DEBUG
  176. #define CHECKGDI(__x)  (WGETCHECKGDIHANDLES?WASSERT(__x):(void)0)
  177. #else
  178. #define CHECKGDI(__x)
  179. #endif
  180.  
  181. //
  182. // System error checking
  183. //
  184.  
  185. #if defined( _DEBUG ) && defined( __BUILDING_WCM__ )
  186. #define WUPDATEERRORLOGFILE() if( WDebug::CheckErrors ) \
  187.                                  WDebug::CheckErrorCode( __WTEXT(__FUNCTION__),\
  188.                                  __WTEXT(__FILE__), __LINE__ )
  189. #elif defined( _DEBUG )
  190. #define WUPDATEERRORLOGFILE() WDebug::CheckErrorCode( __WTEXT(__FUNCTION__),\
  191.                                  __WTEXT(__FILE__), __LINE__ )
  192. #else
  193. #define WUPDATEERRORLOGFILE() ((void)0)
  194. #endif
  195.  
  196. #define WUPDATESYSTEMERRORLOGFILE() WUPDATEERRORLOGFILE()
  197.  
  198. //
  199. // Tracing facilities
  200. //
  201. //    Declare a WTraceMarker statically at the beginning of a function.
  202. //    Then declare a WTrace on the stack, passing in the address of the
  203. //    trace marker.
  204. //
  205.  
  206. class WCMCLASS WTraceMarker {
  207.  
  208.     public:
  209.  
  210.         struct WTraceMarkerEntry {
  211.             wllist_link  link;
  212.  
  213.             const WChar *name;
  214.             const WChar *fileName;
  215.             WULong       line;
  216.         };
  217.  
  218.     public:
  219.         WTraceMarker( const WChar *name, const WChar *fileName,
  220.                       WULong line );
  221.  
  222.         // ~WTraceMarker();   no destructor is needed....
  223.  
  224.         //
  225.         // Data
  226.         //
  227.  
  228.         WTraceMarkerEntry _markerInfo;
  229. };
  230.  
  231. class WCMCLASS WTrace {
  232.  
  233.     public:
  234.  
  235.         typedef WTraceMarker::WTraceMarkerEntry wtm_entry;
  236.  
  237.         struct WTraceEntry {
  238.             wllist_link            link;
  239.             wtm_entry             *marker;
  240.         };
  241.          
  242.     public:
  243.  
  244.         WTrace( WTraceMarker & marker );
  245.  
  246.         ~WTrace();
  247.  
  248.         void Reset( WTraceMarker & marker );
  249.  
  250.         #ifdef _DEBUG
  251.  
  252.         static void PrintCallStack( const WChar *name,
  253.                                     const WChar *fileName, int line );
  254.  
  255.         #endif
  256.  
  257.         WTraceEntry _traceInfo;
  258. };
  259.  
  260. #ifdef _DEBUG
  261.     #define WPRINTCALLSTACK() WTrace::PrintCallStack(__WTEXT(__FUNCTION__),\
  262.                                                      __WTEXT(__FILE__),\
  263.                                                      __LINE__)
  264. #else
  265.     #define WPRINTCALLSTACK()
  266. #endif
  267.  
  268. //
  269. // Debugging control
  270. //
  271. //    Used to turn on/off debugging options.
  272. //
  273.  
  274. class WCMCLASS WDebug {
  275.  
  276.     public:
  277.  
  278.         struct WDebugEntry {
  279.             wllist_link link;
  280.             WULong      memoryFlags;
  281.             WBool       checkGDI;
  282.             WBool       checkStrings;
  283.         };
  284.  
  285.     public:
  286.  
  287.         WDebug();
  288.  
  289.         ~WDebug();
  290.  
  291.         /**************************************************************
  292.          * Properties
  293.          **************************************************************/
  294.  
  295.         // CheckErrors
  296.  
  297.         static WBool GetCheckErrors();
  298.         static WBool SetCheckErrors( WBool check );
  299.  
  300.         // CheckGDIHandles
  301.  
  302.         static WBool GetCheckGDIHandles();
  303.         static WBool SetCheckGDIHandles( WBool check );
  304.  
  305.         // CheckStrings
  306.  
  307.         static WBool GetCheckStrings();
  308.         static WBool SetCheckStrings( WBool check );
  309.  
  310.         // MemoryTracking
  311.  
  312.         static WULong GetMemoryTracking();
  313.         static WBool  SetMemoryTracking( WULong trackFlags );
  314.  
  315.         // ObjectFlags
  316.  
  317.         static WULong GetObjectFlags();
  318.         static WBool  SetObjectFlags( WULong flags );
  319.  
  320.         /**************************************************************
  321.          * Methods
  322.          **************************************************************/
  323.  
  324.         //
  325.         // Assert
  326.         //
  327.  
  328.         static void Assert( const WChar *line1, const WChar *line2,
  329.                             const WChar *line3, const WChar *name,
  330.                             const WChar *fileName, WULong lineNumber );
  331.  
  332.         // BreakIntoDebugger
  333.         //
  334.         //    Enter the debugger if it is present.  Returns FALSE
  335.         //    if the debugger was not entered.  The parameter
  336.         //    is a message to display (it can be NULL).  Only works
  337.         //    in debugging version.
  338.  
  339.         static WBool BreakIntoDebugger( const WAnsiChar *message,
  340.                                         WBool startWDW );
  341.  
  342.         // CheckErrorCode
  343.         //
  344.         //    Used for debugging.  Checks to see if the last error
  345.         //    code is non-zero, and if so prints a debugging message.
  346.         //    It then calls ::SetErrorCode( 0 ) to reset the error.
  347.         //    Returns the error code that was detected.  Fails without
  348.         //    checking the error code if GetCheckErrors() is FALSE.
  349.  
  350.         static WDWord CheckErrorCode( const WChar *functionName,
  351.                                       const WChar *fileName,
  352.                                       WULong line );
  353.  
  354.         //
  355.         // InternalError
  356.         //
  357.  
  358.         static void InternalError( const WChar *text, const WChar *name,
  359.                                    const WChar *fileName, WULong line );
  360.  
  361.         //
  362.         // IsDebuggerPresent
  363.         //
  364.  
  365.         static WBool IsDebuggerPresent();
  366.  
  367.         //
  368.         // NoDefault
  369.         //
  370.  
  371.         static void NoDefault( const WChar *name,
  372.                                const WChar *fileName, WULong lineNumber );
  373.  
  374.         //
  375.         // Printf
  376.         //
  377.  
  378.         static void Printf( const WChar *format, ... );
  379.  
  380.         //
  381.         // ValidateObject
  382.         //
  383.  
  384.         static void ValidateObject( WObject *ptr, WULong len,
  385.                                     const WChar *name,
  386.                                     const WChar *fileName, WULong line );
  387.                                     
  388.  
  389.         /**************************************************************
  390.          * Data
  391.          **************************************************************/
  392.  
  393.     #ifdef __BUILDING_WCM__
  394.     public:
  395.     #else
  396.     private:
  397.     #endif
  398.         WDebugEntry _debugInfo;
  399.  
  400.         static WBool                   CheckStrings;
  401.         static WBool                   CheckGDIHandles;
  402.         static WULong                  MemoryTracking;
  403.         static WULong                  ObjectFlags;
  404.         static WBool                   CheckErrors;
  405. };
  406.  
  407. #define WDEBUG_CHECK_OBJECT_FLAGS 0x01
  408. #define WDEBUG_CHECK_OBJECT_WRITE 0x02
  409. #define WDEBUG_CHECK_OBJECT_READ  0x04
  410.  
  411. #define WDEBUG_CHECK_OBJECT_NONE  0x00
  412. #define WDEBUG_CHECK_OBJECT_ALL   0xFF
  413.  
  414.  
  415. #ifndef _WNO_PRAGMA_PUSH
  416. #pragma enum pop;
  417. #pragma pack(pop);
  418. #endif
  419.  
  420. #endif // _WDEBUG_HPP_INCLUDED
  421.